package com.leon.androidcoursedesign.adapters;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.leon.androidcoursedesign.R;
import com.leon.androidcoursedesign.adapters.baseAdapter.BaseAdapter;
import com.leon.androidcoursedesign.adapters.baseAdapter.BaseViewHolder;
import com.leon.androidcoursedesign.beans.Course;
import com.leon.androidcoursedesign.ui.views.RoundPopupWindow;

import java.util.List;

public class CourseAdapter extends BaseAdapter<Course> {
    private final List<Course> courses;
    private final Context context;

    public CourseAdapter(Context context, List<Course> courses) {
        super(courses, context);
        this.context = context;
        this.courses = courses;
    }

    private OnCourseItemListener onCourseItemListener;

    public interface OnCourseItemListener {
        void refreshCourseDetail(Course course);
        void editCourse(Course course);
        void deleteCourse(String itemId, int position);
    }

    public void setOnCourseItemListener(OnCourseItemListener onCourseItemListener) {
        this.onCourseItemListener = onCourseItemListener;
    }

    @Override
    public int setLayout() {
        return R.layout.course_item;
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        Course course = courses.get(position);

        holder
                .setText(R.id.course_item_textView_name, course.courseName)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onCourseItemListener != null) {
                            onCourseItemListener.refreshCourseDetail(course);
                        }
                    }
                })
                .setLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        RoundPopupWindow roundPopupWindow = new RoundPopupWindow(context, v);
                        roundPopupWindow
                                .setContentView(R.layout.course_item_popup_window)
                                .setOnClickListener(R.id.course_popWindow_textView_edit, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        // 修改对应course数据
                                        if (onCourseItemListener != null) {
                                            onCourseItemListener.editCourse(course);
                                            Log.d("Leon", "CourseAdapter RoundWindow onClick-------position:" + position + "----Course:" + course.toString());
                                        }

                                        roundPopupWindow.dismiss();
                                    }
                                })
                                .setOnClickListener(R.id.course_popWindow_textView_delete, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        // 删除对应course
                                        if (onCourseItemListener != null) {
                                            onCourseItemListener.deleteCourse(course.itemId, position);
                                        }

                                        roundPopupWindow.dismiss();
                                    }
                                })
                                .create();

                        return true;
                    }
                });
    }
}